/**
 * Copyright (C) 2014 Jonas Lammers
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package mygame.utils;

import com.jme3.scene.Geometry;
import java.util.ArrayList;
import java.util.List;
import mygame.CollisionObject;

/**
 *
 * @author jonas
 */
public class PositionSyncer{
    
    private List<CollisionObject> colliderList;
    private List<Geometry> geomList;
    
    public PositionSyncer(){
        this.colliderList = new ArrayList<CollisionObject>();
        this.geomList = new ArrayList<Geometry>();
    }
    
    public void addPair(CollisionObject source, Geometry target){
        this.colliderList.add(source);
        this.geomList.add(target);
    }
    
    public void removePair (Geometry g){
        int index = this.geomList.indexOf(g);
        this.geomList.remove(g);
        this.colliderList.remove(index);
    }
    
    public void removePair (CollisionObject c){
        int index = this.colliderList.indexOf(c);
        this.colliderList.remove(c);
        this.geomList.remove(index);
    }
    
    public void clearList(){
        this.colliderList.clear();
        this.geomList.clear();
    }
    
    public void sync(){
        for(int i = colliderList.size()-1; i>=0; i--){
            CollisionObject c = colliderList.get(i);
            Geometry g = geomList.get(i);
            g.setLocalTranslation((float)c.getPosition().getX(), 
                    (float)c.getPosition().getY(), 
                    (float)c.getPosition().getZ());
        }
    }
}
